import { get } from "./api";

export const userService = {
  listUsers: async (params) => await get("/users", { params }),
  createUser: async (userData) => await post("/users", userData),
  updateUser: async (id, userData) => await put(`/users/${id}`, userData),
  deleteUser: async (id) => await del(`/users/${id}`),
  getCurrentUser: async () => {
    try {
      const response = await get("/users/1");
      console.log("用户接口原始响应数据:", response.data);
      return {
        id: response.data?.userInfo?.username || "guest",
        name: response.data?.userInfo?.username || "访客用户",
        email: response.data?.email || "",
        role: response.data?.role || "guest",
        createdAt: new Date().toISOString(),
      };
    } catch (error) {
      console.error("获取用户数据失败:", {
        url: error.config.url,
        method: error.config.method,
        params: error.config.params,
        responseStatus: error.response?.status,
        errorMessage: error.message,
      });
      return {
        id: "guest",
        name: "访客用户",
        email: "",
        role: "guest",
        createdAt: new Date().toISOString(),
      };
    }
  },
};
